#include <stdio.h>
#include <string.h>
#include "address_book_system.h"
//用户登录功能
int User_Login(int* usernum,seqlist_t* userdata)
{
    FILE* fp;
    int i=1;
    char inputaccount[32]={'\0'};
    char inputpassword[32]={'\0'};
    seqlist_t readuser;

    fp=fopen("userdata.bin","r");//打开存储文件
    if (NULL==fp)
    {
        printf("您没有注册的用户!\n");
        perror("user fopen err\n");
        return -1;
    }

    printf("********您已进入用户登录系统!********\n");
    while (1)
    {
        int flag=0;
        printf("请输入您的帐号:\n");
        scanf("%s",inputaccount);
        fseek(fp,0,SEEK_SET);//归到文件开头
        while (fread(&readuser,sizeof(seqlist_t),1,fp)!=0)
        {
            if (strcmp(readuser.account,inputaccount)==0)
            {
                flag=1;
                break;
            }
            *usernum=i++;
        }
        if (flag==1)
        {
            break;
        }
        printf("您的帐号输入错误,请重新输入!\n");
    }
    while (1)
    {
        printf("请输入您的密码:\n");
        scanf("%s",inputpassword);
        if (strcmp(readuser.password,inputpassword)==0)
        {
            printf("登录成功，欢迎使用雪球通讯录!\n");
            *userdata->account=inputaccount;
            *userdata->password=inputpassword;
            return LOGINSUCCESS;
        }
        printf("密码错误，请重新输入!\n");
    }
    printf("***********************************\n");

    fclose(fp);

    return 0;
}
//用户登出功能
void User_Log_Out()
{
    printf("您的帐号已退出!\n");
}